﻿using System;
using System.Web.Services;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Wicresoft.SelfService.WebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ITSMSync : System.Web.Services.WebService
    {

        [WebMethod]
        public string SyncServiceRecord(string requestID, string status, string approver)
        {
            string excResult = string.Empty;
            UpdateServiceRecords srs = new UpdateServiceRecords();
            try
            {
                if (!string.IsNullOrEmpty(status))
                {
                    srs.updateRequestStatus(requestID, status);
                }
                if (!string.IsNullOrEmpty(approver))
                {
                    srs.updateRequestApprover(requestID, approver);
                }
                excResult = string.Empty;
                return excResult;
            }
            catch(Exception ex)
            {
                excResult = ex.Message;
                return excResult;
            }
        }

        [WebMethod]
        public bool Authentication(string UserName, string Password)
        {
            MySqlConnection conn = null;
            try
            {
                conn = new MySqlConnection(ConfigurationManager.AppSettings["auth"]);
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = conn;
                cmd.CommandText = @"select id from persons where name=@name and password=@password";
                cmd.Parameters.Add(new MySqlParameter("name", UserName));
                cmd.Parameters.Add(new MySqlParameter("password", Password));
                conn.Open();
                object ret = cmd.ExecuteScalar();
                if (ret != null)
                    return true;
                else
                    return false;
            }
            catch(Exception ex)
            {
                LogHelper.Error(ex.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }
 
        }


        //[WebMethod]
        //public string SyncServiceRecord2(UpdateParam param)
        //{
        //    string excResult = string.Empty;
        //    UpdateServiceRecords srs = new UpdateServiceRecords();
        //    try
        //    {
        //        string sql = string.Empty;
        //        LogHelper.Info("param.RequestID:" + param.RequestID + "  param.Status:" + param.Status + "  param.Approver:" + param.Approver + "  param.IncidentNumber:" + param.IncidentNumber);
        //        excResult = srs.updateSelfServiceRequest(param,out sql).ToString();
        //        LogHelper.Info(sql);
        //        return excResult;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.Error(ex.Message);
        //        excResult = ex.Message;
        //        return excResult;
        //    }
        //}

        //[WebMethod]
        //public string SyncServiceRecord3(string requestID, string status, string approver, string incidentNumber)
        //{
        //    string excResult = string.Empty;
        //    UpdateServiceRecords srs = new UpdateServiceRecords();
        //    UpdateParam param = new UpdateParam();
        //    param.RequestID = requestID;
        //    param.Status = status;
        //    param.Approver = approver;
        //    param.IncidentNumber = incidentNumber;
        //    try
        //    {
        //        string sql = string.Empty;
        //        LogHelper.Info("param.RequestID:" + param.RequestID + "  param.Status:" + param.Status + "  param.Approver:" + param.Approver + "  param.IncidentNumber:" + param.IncidentNumber);
        //        excResult = srs.updateSelfServiceRequest(param, out sql).ToString();
        //        LogHelper.Info(sql);
        //        return excResult;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.Error(ex.Message);
        //        excResult = ex.Message;
        //        return excResult;
        //    }
        //}

        //[WebMethod]
        //public string SyncServiceRequestApprover(string requestID, string approver)
        //{
        //    string excResult = string.Empty;
        //    UpdateServiceRecords srs = new UpdateServiceRecords();
        //    try
        //    {
        //        srs.updateRequestApprover(requestID, approver);
        //        excResult = string.Empty;
        //        return excResult;
        //    }
        //    catch (Exception ex)
        //    {
        //        excResult = ex.Message;
        //        return excResult;
        //    }
        //}
    }
}